<?php

declare(strict_types=1);

namespace App\Controller;

use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\AutoController;

/**
 * Class RedisController
 * @package App\Controller
 * @AutoController()
 */
class RedisController extends AbstractController
{

    public function index()
    {
//        $this->container->get(Factory)
        return ['hello' => 'world'];
    }

    public function set(RequestInterface $request)
    {
        $redis = $this->container->get(\Redis::class);
        $key = $request->input('key', sprintf('key_%s', rand(1, 100)));
        $redis->set($key, $key . time());
    }

    public function get(RequestInterface $request, ResponseInterface $response)
    {
        $redis = $this->container->get(\Redis::class);
        $key = $request->input('key', 'key');
        $result = $redis->get($key);
        return $response->raw(var_export($result, true));
    }

    public function all()
    {
//        $this->container->get(\Hyperf\Redis\RedisFactory::class);
        $redis = $this->container->get(\Hyperf\Redis\Redis::class);
        $result = $redis->keys('*');
        return $this->response->raw(var_export($result, true));
    }
}
